<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item  >
        <el-date-picker
          value-format="yyyy"
          v-model="queryParams.year"
          type="year"
          placeholder="选择年">
        </el-date-picker>
      </el-form-item>
      <el-form-item  >
        <el-select v-model="queryParams.objId" clearable  placeholder="请选择员工">
          <el-option
            v-for="item in users"
            :key="item.userId"
            :label="item.nickName"
            :value="item.userId">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
        plain
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crm:achievement:add']"
        >设置目标</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
        plain
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crm:achievement:remove']"
        >删除</el-button>
      </el-col>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="achievementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="员工" align="center" prop="nickName" />
      <el-table-column label="年" align="center" prop="year" />
      <el-table-column label="类型" align="center" prop="status" >
            <template slot-scope="scope">
                  <el-tag prop="status" v-for="(item, index) in statusOptions"  :type="scope.row.status==0?'success':'danger'"  v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
              </template>
          </el-table-column>
      <el-table-column label="一月" align="center" prop="january" />
      <el-table-column label="二月" align="center" prop="february" />
      <el-table-column label="三月" align="center" prop="march" />
      <el-table-column label="四月" align="center" prop="april" />
      <el-table-column label="五月" align="center" prop="may" />
      <el-table-column label="六月" align="center" prop="june" />
      <el-table-column label="七月" align="center" prop="july" />
      <el-table-column label="八月" align="center" prop="august" />
      <el-table-column label="九月" align="center" prop="september" />
      <el-table-column label="十月" align="center" prop="october" />
      <el-table-column label="十一月" align="center" prop="november" />
      <el-table-column label="十二月" align="center" prop="december" />
      
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改业绩目标对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
          <el-form-item label="考核人员" prop="uids">
              <el-select v-model="form.uids" multiple   placeholder="请选择员工">
                <el-option
                  v-for="item in users"
                  :key="item.userId"
                  :label="item.nickName"
                  :value="item.userId">
                </el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="考核年" prop="currentYear">
              <el-date-picker
                v-model="form.currentYear"
                type="year"
                placeholder="选择年">
              </el-date-picker>
          </el-form-item>
           <el-form-item label="年业绩目标金额" prop="yeartarget">
             <el-col :span="11">
                <el-input v-model="form.yeartarget" type="number" placeholder="请输入年业绩目标金额"></el-input>
             </el-col>
              <el-col :span="11">
                 <el-button style="margin-left:10px" @click="setMoney()">平均分配到每月</el-button>
              </el-col>
          </el-form-item>
         
         
      
        <el-form-item label="每一季度" required>
          
          <el-col :span="8">
            <el-form-item   prop="january">
                  <el-input v-model="form.january"   type="number" placeholder="请输入一月" />
            </el-form-item>
          </el-col>
           <el-col :span="8">
              <el-form-item   prop="february">
                  <el-input v-model="form.february"  type="number" placeholder="请输入二月" />
                </el-form-item>
          </el-col>
         <el-col :span="8">
            <el-form-item   prop="march">
                  <el-input v-model="form.march"  type="number" placeholder="请输入三月" />
              </el-form-item>
          </el-col>
         
        </el-form-item>
         <el-form-item label="每二季度" required>
              <el-col :span="8">
                 <el-form-item   prop="april">
                      <el-input v-model="form.april"  type="number" placeholder="请输入四月" />
                  </el-form-item>  
              </el-col>
              <el-col :span="8">
                 <el-form-item   prop="may">
                      <el-input v-model="form.may"  type="number" placeholder="请输入五月" />
                  </el-form-item>  
              </el-col>
            <el-col :span="8">
                <el-form-item   prop="june">
                      <el-input v-model="form.june"  type="number" placeholder="请输入六月" />
                </el-form-item>  
              </el-col>
        </el-form-item>
         <el-form-item label="每三季度" required>
              <el-col :span="8">
                <el-form-item   prop="july">
                      <el-input v-model="form.july"  type="number" placeholder="请输入七月" />
                  </el-form-item>  
              </el-col>
              <el-col :span="8">
                <el-form-item   prop="august">
                      <el-input v-model="form.august"  type="number" placeholder="请输入八月" />
                  </el-form-item>  
              </el-col>
              <el-col :span="8">
                  <el-form-item   prop="september">
                      <el-input v-model="form.september"  type="number" placeholder="请输入九月" />
                    </el-form-item>  
              </el-col>
        </el-form-item>
         <el-form-item label="每四季度" required >
              <el-col :span="8">
                <el-form-item   prop="october">
                      <el-input v-model="form.october"  type="number" placeholder="请输入十月" />
                </el-form-item>  
              </el-col>
              <el-col :span="8">
                <el-form-item   prop="november">
                      <el-input v-model="form.november"  type="number" placeholder="请输入十一月" />
                   </el-form-item>  
              </el-col>
            <el-col :span="8">
                <el-form-item   prop="december">
                      <el-input v-model="form.december"  type="number" placeholder="请输入十二月" />
                 </el-form-item>  
              </el-col>
        </el-form-item>
        
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAchievement, getAchievement, delAchievement, addAchievement, updateAchievement, exportAchievement } from "@/api/crm/achievement";
import { getAllList} from "@/api/system/user";

export default {
  name: "Achievement",
  data() {
    return {
      statusOptions:[],
      users:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 业绩目标表格数据
      achievementList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        objId: null,
        type: null,
        year: null,
        january: null,
        february: null,
        march: null,
        april: null,
        may: null,
        june: null,
        july: null,
        august: null,
        september: null,
        october: null,
        november: null,
        december: null,
        status: null,
        yeartarget: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        uids: [
          { required: true, message: "请选择员工", trigger: "blur" }
        ],
        currentYear: [
          { required: true, message: "请选择年", trigger: "blur" }
        ],
        yeartarget: [
          { required: true, message: "请输入年考核金额", trigger: "blur" }
        ],
        january: [
          { required: true, message: "请输入一月金额", trigger: "blur" }
        ],
        february: [
          { required: true, message: "请输入二月金额", trigger: "blur" }
        ],
        march: [
          { required: true, message: "请输入三月金额", trigger: "blur" }
        ],
        april: [
          { required: true, message: "请输入四月金额", trigger: "blur" }
        ],
        may: [
          { required: true, message: "请输入五月金额", trigger: "blur" }
        ],
        june: [
          { required: true, message: "请输入六月金额", trigger: "blur" }
        ],
        july: [
          { required: true, message: "请输入七月金额", trigger: "blur" }
        ],
        august: [
          { required: true, message: "请输入八月金额", trigger: "blur" }
        ],
        september: [
          { required: true, message: "请输入九月金额", trigger: "blur" }
        ],
        october: [
          { required: true, message: "请输入十月金额", trigger: "blur" }
        ],
        november: [
          { required: true, message: "请输入十一月金额", trigger: "blur" }
        ],
        december: [
          { required: true, message: "请输入十二月金额", trigger: "blur" }
        ],
        
      }
    };
  },
  created() {
    this.getList();
    this.getAllUsers();
  },
  methods: {
    setMoney(){
        var money=this.form.yeartarget/12;
        this.form.january=money.toFixed(2);
        this.form.february=money.toFixed(2);
        this.form.march=money.toFixed(2);
        this.form.april=money.toFixed(2);
        this.form.may=money.toFixed(2);
        this.form.june=money.toFixed(2);
        this.form.july=money.toFixed(2);
        this.form.august=money.toFixed(2);
        this.form.september=money.toFixed(2);
        this.form.october=money.toFixed(2);
        this.form.november=money.toFixed(2);
        this.form.december=money.toFixed(2);

    },
    getAllUsers() {
      var data={};
      getAllList(data).then(response => {
        this.users = response.rows;
      });
      this.getDicts("achievement_status").then((response) => {
        this.statusOptions = response.data;
      });
      
    },
    /** 查询业绩目标列表 */
    getList() {
      this.loading = true;
      listAchievement(this.queryParams).then(response => {
        this.achievementList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uids: [],
        currentYear:null,
        achievementId: null,
        objId: null,
        type: null,
        year: null,
        january: null,
        february: null,
        march: null,
        april: null,
        may: null,
        june: null,
        july: null,
        august: null,
        september: null,
        october: null,
        november: null,
        december: null,
        status: 0,
        yeartarget: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.achievementId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加业绩目标";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const achievementId = row.achievementId || this.ids
      getAchievement(achievementId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改业绩目标";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.achievementId != null) {
            // updateAchievement(this.form).then(response => {
            //   if (response.code === 200) {
            //     this.msgSuccess("修改成功");
            //     this.open = false;
            //     this.getList();
            //   }
            // });
          } else {
            addAchievement(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const achievementIds = row.achievementId || this.ids;
      this.$confirm('是否确认删除业绩目标编号为"' + achievementIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delAchievement(achievementIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有业绩目标数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportAchievement(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
<style scoped>
.el-form-item{
  padding: 5px;
}
</style>
